'use strict';
'require view';
'require ui';
'require form';
'require rpc';
'require tools.widgets as widgets';

var callServiceList = rpc.declare({
	object: 'service',
	method: 'list',
	params: ['name'],
	expect: {
		'': {}
	}
});

function getServiceStatus() {
	return L.resolveDefault(callServiceList('idc_mesh'), {})
		.then(function(res) {
			var isRunning = false;
			try {
				isRunning = res['idc_mesh']['instances']['instance1']['running'];
			} catch (e) {}
			return isRunning;
		});
}
function renderStatus(isRunning) {
	var renderHTML = "";
	var spanTemp = '<em><span style="color:%s"><strong>%s %s</strong></span></em>';
	if (isRunning) {
		renderHTML += String.format(spanTemp, 'green', _("MESH模块"), _("RUNNING"));
	} else {
		renderHTML += String.format(spanTemp, 'red', _("MESH模块"), _("NOT RUNNING"));
	}
	return renderHTML;
}

return view.extend({
    load: function() {
        // return Promise.all([callGetEntryInfo(),callGetLoraTrans(),callGetLoraAddr()]);;
    },
	openCannel: function(param){
	},
    render:function(data){
        var m,s,pass;
        m=new form.Map('idc_mesh','MESH模块配置');
        s = m.section(form.NamedSection, '_status');
		s.anonymous = true;
		s.render = function(section_id) {
			L.Poll.add(function() {
				return L.resolveDefault(getServiceStatus())
					.then(function(res) {
						var view = document.getElementById("service_status");
						view.innerHTML = renderStatus(res);
					});
			});
			return E('div', {
				class: 'cbi-map'
			}, E('fieldset', {
				class: 'cbi-section'
			}, [E('p', {
				id: 'service_status'
			}, _('Collecting data ...'))]));
		}
        s = m.section(form.NamedSection, 'mesh', 'conf');
        s.anonymous = true;
        s.addremove = false;
        

        s.tab("mesh_cfg", "Mesh配置")
        //两个参数，分别是rip和rport
        s.taboption("mesh_cfg", form.Value, "ipaddr", "模块IP:", "")
        s.taboption("mesh_cfg", form.Value, "username", "用户名:", "")
        pass = s.taboption("mesh_cfg", form.Value, "password", "密码:", "")
        pass.password = true;

        return m.render();
    },
    // handleSaveApply: null,
//    handleSave: null,
//    handleReset: null
});